<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends MY_Luckydog
{
    public function __construct()
    {
        parent::__construct();
        
        $this->load->model('luckydog/News_model', 'news');
    }

/**
 * 新闻列表
 * @return [type] [description]
 */
    public function index(){
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $_SESSION['v_menu'] = 'news';
        $filter = array("type"=>$type);
        $limit = 20;
        $total_rows = $this->news->getNewsCnt($filter,$per_page,$limit);
        $base_url = '/luckydog/news/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $newsList = $this->news->getNewsList($filter,$per_page, $limit);
        $data = array(
            'newsList'=>$newsList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'type'=>$type
            );
        $this->load->view("/luckydog/news/index", $data);
    }

    public function textures()
    {
        $id = $this->input->get("id",true)?$this->input->get("id",true):"";
        if ($login_status === FALSE){
            header("Location:login");
        }
        $_SESSION['v_menu'] = 'news';
        $data = array();
        if(!empty($id)){
            $NewsInfo = $this->news->getNewsInfobyID($id);
            $data["NewsInfo"] = $NewsInfo;
        }
        $this->load->view("/luckydog/news/textures", $data);
    }



    //编辑新闻
    public function edit(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $content = $this->input->post("content",true) ? $this->input->post("content",true) : "";
        $title = $this->input->post("title",true) ? $this->input->post("title",true) : "";
        $data = array(
            "content"=>$content,
            "title"=>$title,
        );
        if(empty($id)){
            $data['add_time']=time();
        }
        $res = $this->news->editNews($id,$data);
        echo $res;
    }


    //编辑上线下，删除状态
    public function delNews(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res =$this->news->delNews($id);
        echo $res;
    }

    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);

        $savepath='./upload/news/'.date('Y-m-d').'/';
        $upload->set_upload_dir($savepath);
        if (!file_exists($savepath)){
            mkdir($savepath);
        }
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */